﻿using System;
using System.Collections.Generic;
using System.Text;


/// <summary>
/// 循环顺序队列
/// </summary>
public class CSeqQueue<T> : IQueue<T>
{
    private int maxsize; // 循环队列的容量
    private T[] data; // 数组
    private int front;//队头
    private int rear;//队尾

    public T this[int index]
    {
        get { return data[index]; }
        set { data[index] = value; }
    }

    public int Maxsize { get { return maxsize; } set { maxsize = value; } }
    public int Front { get { return front; } set { front = value; } }
    public int Rear { get { return rear; } set { rear = value; } }

    public CSeqQueue(int size)
    {
        data = new T[size];
        maxsize = size;
        front = rear = 0;
    }


    public void Clear()
    {
        front = rear = 0;
    }

    public T GetFront()
    {
        if (IsEmpty())
        {
            Console.WriteLine("Queue is empty");
            return default(T);
        }
        return data[front];
    }

    public int GetLength()
    {
        return (rear - front + maxsize) % maxsize;
    }

    public void In(T item)
    {
        if (IsFull())
        {
            Console.WriteLine("Queue is Full");
            return;
        }

        data[rear] = item;
        rear = (rear + 1) % maxsize;
    }

    public bool IsEmpty()
    {
        return front == rear;
    }

    public bool IsFull()
    {
        if ((rear + 1) % maxsize == front)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public T Out()
    {
        T tmp = default(T);
        if (IsEmpty())
        {
            Console.WriteLine("Queue is empty");
            return tmp;
        }
        tmp = data[front];
        front = (front + 1) % maxsize;
        return tmp;
    }
}
